//
//  STLoginController.m
//  ShaBaoOnLine
//
//  Created by huang yong on 16/7/7.
//  Copyright © 2016年 com.ShaBaoOnline.www. All rights reserved.
//

#import "STLoginController.h"

#import "STRegisterController.h"
//#import "STForgetPassWordController.h"
#import "STTabBarController.h"
//#import "STNetworkRequest.h"
//#import "MJExtension.h"
#import "AppDelegate.h"
#import "STNavigationController.h"
#import "AskForIntoViewController.h"
@interface STLoginController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *rememberBtn;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *height1;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *height2;

@end

@implementation STLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self applyNameAndPassDataFromDefaults];
    [self.navigationController setNavigationBarHidden:YES];
    self.userNameTF.delegate = self;
    self.passwordTF.delegate = self;
   
}
#pragma mark - XMPPStreamDelegate
//登陆成功
- (void)loginSuccess{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.userNameTF.text forKey:@"userName"];
    [defaults setObject:self.passwordTF.text forKey:@"password"];
    STTabBarController *tabBarVc = [[STTabBarController alloc] init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
}

- (void)viewWillLayoutSubviews {
    self.height1.constant = 62 * HEIGHT_SCAL;
    self.height2.constant = 115 * HEIGHT_SCAL ;
    
    [super viewWillLayoutSubviews];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES];
    [self applyNameAndPassDataFromDefaults];
    [super viewWillAppear:animated];
}
- (void)applyNameAndPassDataFromDefaults {
    //从数据库中获取用户名／密码
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"userName"];
    NSString *password = [defaults objectForKey:@"password"];
    NSNumber *isSeleted = [defaults objectForKey:@"remember"];
    _passwordTF.text = nil;
    
    //是否选中记住密码
    self.rememberBtn.selected = [isSeleted boolValue];
    if(nil != userName){
        _userNameTF.text = userName;
        //是否显示密码
        
        if ([isSeleted boolValue]) {
            _passwordTF.text = password;
        }
    }
    if (_noPassword) {
        _passwordTF.text = nil;
    }
    if (_noPhoneNum) {
        _userNameTF.text = nil;
    }
    
}

#pragma mark ------------------------ 记住密码

- (IBAction)rememberAction:(UIButton *)sender {
    self.rememberBtn.selected = self.rememberBtn.selected ? NO : YES;
    
    //记录按钮选中状态
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int isSeleted = self.rememberBtn.selected ? 1 : 0;
    [defaults setObject:@(isSeleted) forKey:@"remember"];
}

- (IBAction)loginAction {
//    STTabBarController *tabBarVc = [[STTabBarController alloc] init];
//    
//    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
//
//    NSString * userNameStr = [self.userNameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//    NSString * passwordStr = [self.passwordTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//    
//    if (userNameStr.length == 0) {
//        [MBProgressHUD showError:@"手机号不能为空"];
//        return ;
//    }
//    if (![userNameStr validateMobile]) {
//        [MBProgressHUD showError:@"手机号错误"];
//        return ;
//    }
//    
//    if (passwordStr.length == 0) {
//        [MBProgressHUD showError:@"密码不能为空"];
//        return ;
//    }
//    if (![passwordStr validatePassword]) {
//        [MBProgressHUD showError:@"密码必须大于5位且包含数字和字母"];
//        return;
//    }
//    
//    NSMutableDictionary *requestParameter = [NSMutableDictionary dictionary];
//    requestParameter[@"userName"] = userNameStr;
//    requestParameter[@"password"] = [passwordStr MD5];
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    
//    [[STNetworkRequest shareRequest] startGetRequest:api_user_login parameter:requestParameter showNetworkErrorAlert:YES onCompletion:^(id jsonResponse) {
//        
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        int result = [[jsonResponse objectForKey:@"result"] intValue];
//        
//        if (result == 1) {
//        
//            [MBProgressHUD showSuccess:@"登录成功"];
//            
//            loginSalerInfo = [ClientRankModel mj_objectWithKeyValues:[jsonResponse objectForKey:@"msg"]];
//            token = [[jsonResponse objectForKey:@"msg"] objectForKey:@"token"];
//            isTokenTimeOut = NO;
//            
//             [ClientRankModel insertSalerPeople:loginSalerInfo];
//            //将用户名密码存储在数据库中
//            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//            [defaults setObject:loginSalerInfo.MOBILENO forKey:@"userName"];
//            [defaults setObject:self.passwordTF.text forKey:@"password"];
//            
//            [defaults setObject:token forKey:@"token"];
//            [defaults setObject:loginSalerInfo.AUTH_FLAG forKey:@"auth_flag"];
//            [defaults synchronize];
//            
//            //将用户数据插入到数据库
//           
//            STTabBarController *tabBarVc = [[STTabBarController alloc] init];
//            
//            [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
//        } else {
////            [MBProgressHUD showError:@"登录失败"];
//        }
//        
//    } onError:^(id jsonResponse) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        [MBProgressHUD showError:@"网络未连接,登陆失败"];
//    }];
    //1. 设置通信管道的目标地址和端口-> 2. 设置通信管道的创建者 -> 3. 链接目标服务器
//    [[XMPPManager sharedManager] xmppManagerLoginWithUserName:self.userNameTF.text password:self.passwordTF.text];
    [self loginSuccess];
}

#pragma mark ------------------------ UItextfield
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _userNameTF) {
        if (textField.text.length == 1) {
             _passwordTF.text = nil;
            self.rememberBtn.selected = NO;
            return YES;
        }
        if (string.length == 0 ) {
            return YES;
        }
        if (textField.text.length - range.length + string.length > 11) {
            return NO;
        }
    }
    if (textField == _passwordTF) {
        if (string.length == 0) {
            return YES;
        }
        if (textField.text.length - range.length + string.length > 20) {
            return NO;
        }
    }
    return YES;
}
-(BOOL)textFieldShouldClear:(UITextField *)textField{
    textField.text = nil;
    _passwordTF.text = nil;
    self.rememberBtn.selected = NO;
    return NO;
}
- (IBAction)retisterAction {
    
    AskForIntoViewController *registerVc = [UIStoryboard loadStoryboard:AskForIntoViewControllerID];
    UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:registerVc];
//    [self.navigationController pushViewController:nv animated:YES];
    [self presentViewController:nv animated:YES completion:nil];
}

- (IBAction)forgetPassWordAction {
    
//    STForgetPassWordController *forgetVc = [[STForgetPassWordController alloc] init];
//    [self.navigationController pushViewController:forgetVc animated:YES];
}

@end
